package com.test.ds.link.interiterator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InterIterApp {

	public static void main(String[] args) throws IOException {
		LinkList theList = new LinkList();
		ListIterator iter1 = theList.getIterator();
		long value;
		
		iter1.insertAfter(20);
		iter1.insertAfter(40);
		iter1.insertAfter(80);
		iter1.insertBefore(60);
		
		while(true){
			System.out.print("Enter first letter of show, reset, ");
			System.out.print("new, get, before, after,delete: ");
			System.out.flush();
			int choise = getChar();
			switch(choise){
			case 's':
				if(!theList.isEnpty()){
					theList.displayList();
				}else{
					System.out.println("List is empty");
				}
				break;
			case 'r':
				iter1.reset();
				break;
			case 'n':
				if(!theList.isEnpty() && !iter1.atEnd()){
					iter1.nextLink();
				}else{
					System.out.println("Can't go to next link");
				}
				break;
			case 'g':
				if(!theList.isEnpty()){
					value = iter1.getCurrent().dData;
					System.out.println("Returned " +value);
				}else{
					System.out.println("List is empty");					
				}
				break;
			case 'b':
				System.out.println("Enter value to insert: ");					
				System.out.flush();
				value = getInt();
				iter1.insertBefore(value);
				break;
			case 'a':
				System.out.println("Enter value to insert: ");					
				System.out.flush();
				value = getInt();
				iter1.insertAfter(value);
				break;
			case 'd':
				if(!theList.isEnpty()){
					value = iter1.deleteCurrent();
					System.out.println("Deleted " + value);					
				}else{
					System.out.println("Can't delete " );						
				}
				break;
			default:
				System.out.println("Invalid entry " );						
			}
		}
	}

	private static String getString() throws IOException {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		String s = br.readLine();
		return s;
	}
	
	private static int getInt() throws IOException {
		String s = getString();
		return Integer.parseInt(s);
	}

	private static char getChar() throws IOException {
		String s = getString();
		return s.charAt(0);
	}
	
	
}
